﻿
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Labthink.PCSoft.UniversalHost4.UserControls;

namespace Report.Forms
{
    /// <summary>
    /// FrmInputValues.xaml 的交互逻辑
    /// </summary>
    public partial class FrmInputValues 
    {
        public FrmInputValues()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
        }

        private void FrmInputValues_OnLoaded(object sender, RoutedEventArgs e)
        {
            GridItems.Children.Clear();
            var manager = ReportManager1.Create();
            var c = manager.AddInputs.Count;
            for (int i = 0; i < c; i++)
            {
                GridItems.RowDefinitions.Add(new RowDefinition{Height=new GridLength(50)});
            }
            var index = 0;
            foreach (var sd in manager.ParamSelected.Where(p=>!string.IsNullOrEmpty(p.Text)))
            {
                var tb = new UcTextBox { Label=sd.Text, MaxLength = 20, Value = ""};
                Grid.SetRow(tb,index);
                GridItems.RegisterName("txt" + sd.Id, tb);
                GridItems.Children.Add(tb);
              
                index++;
            }
            foreach (var sd in manager.ResultSelected.Where(p => !string.IsNullOrEmpty(p.Text)))
            {
                var tb = new UcTextBox { Label = sd.Text, MaxLength = 20, Value = "" };
                Grid.SetRow(tb, index);
                GridItems.RegisterName("txt" + sd.Id, tb);
                GridItems.Children.Add(tb);
                index++;
            }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var manager = ReportManager1.Create();
            foreach (var key  in manager.AddInputs.Keys.ToList())
            {
                var txt = GridItems.FindName("txt" + key) as UcTextBox;
                if (txt != null)
                {
                    manager.AddInputs[key] = txt.Value;
                    var pi = manager.ParamInfo.SingleOrDefault(p => p.DataNo == key);
                    if (pi != null) pi.Value = txt.Value;
                    pi = manager.ResultInfo.SingleOrDefault(p => p.DataNo == key);
                    if (pi != null) pi.Value = txt.Value;
                }
            }
            Close();
        }
    }
}
